package com.zxht.wuchechenyun.activity;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.zxht.wuchechenyun.R;
import com.zxht.wuchechenyun.adapter.MyFragmentPagerAdapter;
import com.zxht.wuchechenyun.fragment.OrdAllFragment;
import com.zxht.wuchechenyun.fragment.OrdDistrFragment;
import com.zxht.wuchechenyun.fragment.OrdEvaluateFragment;
import com.zxht.wuchechenyun.fragment.OrdFinishFragment;
import com.zxht.wuchechenyun.fragment.OrdLanShouFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 找货源
 */
public class FindCargoActivity extends BaseActivity {

    private ViewPager task_viewpager;
    private View task_indicate_line;
    private TextView tv_all;
    private TextView tv_lanshou;
    private MyFragmentPagerAdapter adapter;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_cargo);
    }

    @Override
    public int getLayoutResID() {
        return R.layout.activity_find_cargo;
    }

    @Override
    public void initView() {

        task_viewpager = (ViewPager) findViewById(R.id.task_viewpager);
        task_indicate_line = findViewById(R.id.task_indicate_line);

        tv_all = (TextView) findViewById(R.id.tv_all);
        tv_lanshou = (TextView) findViewById(R.id.tv_lanshou);
        initSubFragment();
    }

    private void initSubFragment() {
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                fragmentList);
        task_viewpager.setAdapter(adapter);
        task_viewpager
                .addOnPageChangeListener(new OnMsgSetPageChangeListener());
        // 初始化数据源
        fragmentList.add(new OrdAllFragment());
        fragmentList.add(new OrdLanShouFragment());
        // 刷新adapter
        adapter.notifyDataSetChanged();
        // 初始化指示器的宽度
        initIndiateWidth();
        task_viewpager.setCurrentItem(0);
        tv_all.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.green));
    }

    private void initIndiateWidth() {
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        task_indicate_line.getLayoutParams().width = screenWidth
                / fragmentList.size();
        // 刷新：
        task_indicate_line.invalidate();
    }


    @Override
    public void initListener() {
        tv_all.setOnClickListener(this);
        tv_lanshou.setOnClickListener(this);
    }

    @Override
    public void onInnerClick(View v) {
        super.onInnerClick(v);
        switch (v.getId()){
            //全部
            case R.id.tv_all:
                task_viewpager.setCurrentItem(0);
                break;
            //待揽收
            case R.id.tv_lanshou:
                task_viewpager.setCurrentItem(1);
                break;

        }

    }

    private final class OnMsgSetPageChangeListener implements
            ViewPager.OnPageChangeListener {
        @Override

        public void onPageSelected(int position) {
            int green = ContextCompat.getColor(getApplicationContext(),R.color.green);
            int black = ContextCompat.getColor(getApplicationContext(),R.color.fount_color);
            tv_all.setTextColor(position == 0 ? green : black);
            tv_lanshou.setTextColor(position == 1 ? green : black);

        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            /**
             * 偏移距离：百分比*指示器的宽度 起始位置：position*指示器的宽度 最终位置：起始位置+偏移距离
             */
            float offsetX = positionOffset * task_indicate_line.getWidth();
            float startX = position * task_indicate_line.getWidth();
            float endX = startX + offsetX;
            ViewHelper.setTranslationX(task_indicate_line, endX);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
